<?php
	require_once(dirname(__FILE__).'/route_pattern.php');
	require_once(dirname(__FILE__).'/router.php');

	class RouterFactory {
		public function createFromIniFile($file) {
			$router = $this->createRouter();
			foreach(parse_ini_file($file, true) as $pattern => $parameters) {
				if($pattern == '?') {
					$router->setDefaultParameters($parameters);
				} else {
					$pattern = $this->createPattern($pattern);
					foreach($parameters as $part => $value) {
						$name = RoutePattern::getPlaceHolderName($part);
						if($name!==false) {
							$pattern->setRegexRuleFor($name, $value);
						} else {
							$pattern->setDefaultFor($part, $value);
						}
					}
					$router->addPattern($pattern);
				}
			}
			return $router;
		}
		
		protected function createPattern($pattern) {
			return new RoutePattern($pattern);
		}
		
		protected function createRouter() {
			return new Router();
		}
	}
?>